/*&h{*/
#ifndef DXF_ELEMENT

#ifndef NULL
#include <stdio.h>
#endif /* NULL */

#ifndef FALSE
#include "\mylib\types.h"
#endif /* FALSE */


#define DXF_ELEMENT 0
#define DXF_TEXT 1
#define DXF_NAME 2
#define DXF_FILENAME 3
#define DXF_BESCHREIBUNG 3
#define DXF_LINIENTYP 6
#define DXF_LAYER 8
#define DXF_VARNAME 9

#define DXF_X 10
#define DXF_Y 20
#define DXF_Z 30

#define DXF_FAKTOR 40
#define DXF_LAENGE 40
#define DXF_TEXTHEIGHT 40
#define DXF_BREITEFAKT 41
#define DXF_EINZELLAENGE 49

#define DXF_WINKEL 50
#define DXF_TEXTANG 50

#define DXF_FARBE 62

#define DXF_GANZZAHL 70
#define DXF_ANZAHL 70
#define DXF_FLAGS 70

/*&h}*/

/************************************************************************/
/* Allgemeine Funktionen                                                */
/************************************************************************/

/*&hf*/
void DxfWriteString(FILE *fp, int GroupID, char *s)
	{
	fprintf(fp, "%d\n%s\n", GroupID, s);
	}

/*&hf*/
void DxfWriteInt(FILE *fp, int GroupID, int i)
	{
	fprintf(fp, "%d\n%d\n", GroupID, i);
	}

/*&hf*/
void DxfWriteDouble(FILE *fp, int GroupID, double d)
	{
	fprintf(fp, "%d\n%g\n", GroupID, d);
	}


/************************************************************************/
/* Abschnitte                                                           */
/************************************************************************/

/*&hf*/
void DxfStartAbschnitt(FILE *fp, char *Abschnitt)
	{
	DxfWriteString(fp, DXF_ELEMENT, "SECTION");
	DxfWriteString(fp, DXF_NAME, Abschnitt);
	}


/*&hf*/
void DxfEndeAbschnitt(FILE *fp)
	{
	DxfWriteString(fp, DXF_ELEMENT, "ENDSEC");
	}


/*&hf*/
void DxfEndeDatei(FILE *fp)
	{
	DxfWriteString(fp, DXF_ELEMENT, "EOF");
	}


/*&hf*/
void DxfStartKopfAbs(FILE *fp)
	{
	DxfStartAbschnitt(fp, "HEADER");
	}


/*&hf*/
void DxfStartTabAbs(FILE *fp)
	{
	DxfStartAbschnitt(fp, "TABLES");
	}


/*&hf*/
void DxfStartElementeAbs(FILE *fp)
	{
	DxfStartAbschnitt(fp, "ENTITIES");
	}


/************************************************************************/
/* Elemente des Kopf-Abschnitts                                         */
/************************************************************************/

/*&hf*/
void DxfLimiten(FILE *fp,
				double xmin, double ymin, double xmax, double ymax)
	{
	DxfWriteString(fp, DXF_VARNAME, "$EXTMIN");
	DxfWriteDouble(fp, DXF_X, xmin);
	DxfWriteDouble(fp, DXF_Y, ymin);
	DxfWriteString(fp, DXF_VARNAME, "$EXTMAX");
	DxfWriteDouble(fp, DXF_X, xmax);
	DxfWriteDouble(fp, DXF_Y, ymax);
	}


/*&hf*/
void DxfLtFaktor(FILE *fp, double f)
	{
	DxfWriteString(fp, DXF_VARNAME, "$LTSCALE");
	DxfWriteDouble(fp, DXF_FAKTOR, f);
	}


/************************************************************************/
/* Tabellen                                                             */
/************************************************************************/

/*&hf*/
void DxfStartTabelle(FILE *fp, char *Name, int Anz)
	{
	DxfWriteString(fp, DXF_ELEMENT, "TABLE");
	DxfWriteString(fp, DXF_NAME, Name);
	DxfWriteInt(fp, DXF_ANZAHL, Anz);
	}


/*&hf*/
void DxfEndeTabelle(FILE *fp)
	{
	DxfWriteString(fp, DXF_ELEMENT, "ENDTAB");
	}


/*&hf*/
void DxfLayerTabelle(FILE *fp, int Anz)
	{
	DxfStartTabelle(fp, "LAYER", Anz);
	}


/*&hf*/
void DxfDefLayer(FILE *fp,
				 char *Name, int Farbe, char *LinienTyp, bool Gefroren)
	{
	DxfWriteString(fp, DXF_ELEMENT, "LAYER");
	DxfWriteString(fp, DXF_NAME, Name);
	DxfWriteInt(fp, DXF_FLAGS, 64 + (Gefroren ? 1 : 0));
	DxfWriteInt(fp, DXF_FARBE, Farbe);
	DxfWriteString(fp, DXF_LINIENTYP, LinienTyp);
	}


/*&hf*/
void DxfStilTabelle(FILE *fp, int Anz)
	{
	DxfStartTabelle(fp, "STYLE", Anz);
	}


/*&hf*/
void DxfDefStil(FILE *fp, char *Name, char *FontName,
				double Hoehe, double BreiteFakt, double Neigung,
				bool Vertikal)
	{
	DxfWriteString(fp, DXF_ELEMENT, "STYLE");
	DxfWriteString(fp, DXF_NAME, Name);
	DxfWriteInt(fp, DXF_FLAGS, 64 + (Vertikal ? 4 : 0));
	DxfWriteDouble(fp, DXF_TEXTHEIGHT, Hoehe);
	DxfWriteDouble(fp, DXF_BREITEFAKT, BreiteFakt);
	DxfWriteDouble(fp, DXF_TEXTANG, Neigung);
	DxfWriteInt(fp, DXF_FLAGS+1, 0);
	DxfWriteDouble(fp, DXF_TEXTHEIGHT+2, 5.0);
	DxfWriteString(fp, DXF_FILENAME, FontName);
	DxfWriteString(fp, DXF_FILENAME+1, "");
	}


/*&hf*/
void DxfLinienTypenTabelle(FILE *fp, int Anz)
	{
	DxfStartTabelle(fp, "LTYPE", Anz);
	}


/*&hf*/
void DxfDefLinienTyp(FILE *fp, char *Name, char *Beschr,
					 int LenTabLen, double *LenTab)
	{
	double GesLen;
	int i;

	GesLen = 0.0;
	for (i = 0 ; i < LenTabLen ; i++)
		GesLen += (LenTab[i] >= 0.0) ? LenTab[i] : -LenTab[i];
	DxfWriteString(fp, DXF_ELEMENT, "LTYPE");
	DxfWriteString(fp, DXF_NAME, Name);
	DxfWriteInt(fp, DXF_FLAGS, 64);
	DxfWriteString(fp, DXF_BESCHREIBUNG, Beschr);
	DxfWriteInt(fp, DXF_FLAGS+2, 65);
	DxfWriteInt(fp, DXF_ANZAHL+3, LenTabLen);
	DxfWriteDouble(fp, DXF_LAENGE, GesLen);
	for (i = 0 ; i < LenTabLen ; i++)
		DxfWriteDouble(fp, DXF_EINZELLAENGE, LenTab[i]);
	}


/************************************************************************/
/* Elemente                                                             */
/************************************************************************/


/*&hf*/
void DxfLinie(FILE *fp,
			  double x1, double y1, double x2, double y2,
			  char *layer, char *lintyp)
	{
	DxfWriteString(fp, DXF_ELEMENT, "LINE");
	if (lintyp != NULL)
		DxfWriteString(fp, DXF_LINIENTYP, lintyp);
	if (layer != NULL)
		DxfWriteString(fp, DXF_LAYER, layer);
	DxfWriteDouble(fp, DXF_X+0, x1);
	DxfWriteDouble(fp, DXF_Y+0, y1);
	DxfWriteDouble(fp, DXF_X+1, x2);
	DxfWriteDouble(fp, DXF_Y+1, y2);
	}


/*&hf*/
void DxfText(FILE *fp, char *s,
			 double x, double y, double ang, double h, char *layer)
	{
	DxfWriteString(fp, DXF_ELEMENT, "TEXT");
	DxfWriteString(fp, DXF_TEXT, s);
	if (layer != NULL)
		DxfWriteString(fp, DXF_LAYER, layer);
	DxfWriteDouble(fp, DXF_X, x);
	DxfWriteDouble(fp, DXF_Y, y);
	DxfWriteDouble(fp, DXF_TEXTHEIGHT, h);
	DxfWriteDouble(fp, DXF_TEXTANG, ang);
	}


/*&h*/
#endif /* DXF_ELEMENT */
